package com.example.seven_service

import android.app.Service
import android.content.Intent
import android.media.MediaPlayer
import android.os.Binder
import android.os.IBinder
import android.provider.MediaStore
import android.util.Log


class MusicService : Service() {
    companion object {
        val Commond="Operate"
    }
    val TAG = "MusicService"

    val musicList = mutableListOf<String>()
    val musicNameList = mutableListOf<String>()
    val mediaPlayer = MediaPlayer()
    var current = 0
    var isPause = false
    var isAutoPlayNext = false
    val binder = MusicBinder()
    inner class MusicBinder : Binder() {
        val musicName: String
            get() = musicNameList.get(current)
        var currentPosition: Int
            get() = mediaPlayer.currentPosition
            set(value) = mediaPlayer.seekTo(value)
        val duration: Int
            get() = mediaPlayer.duration
        val size: Int
            get() = musicList.size
        val currentIndex: Int
            get() = current
    }

    override fun onCreate() {
        super.onCreate()
        getMusicList()
        mediaPlayer.setOnCompletionListener {
            if (isAutoPlayNext) {
                current++
                if (current >= musicList.size) {
                    current = 0
                }
                play()
            }
        }
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val operate=intent?.getIntExtra(Commond,1) ?:1
        when(operate){
            1->play()
            2->pause()
            3->stop()
            4->next()
            5->prev()
        }


        return super.onStartCommand(intent, flags, startId)
    }


    fun pause() {
        if (isPause) {
            mediaPlayer.start()
            isPause = false
        } else {
            mediaPlayer.pause()
            isPause = true
        }
    }

    fun stop() {
        mediaPlayer.stop()
        stopSelf()
    }

    fun next() {
        current++
        if (current >= musicList.size) {
            current = 0
        }
        play()
    }

    fun prev() {
        current--
        if (current < 0) {
            current = musicList.size - 1
        }
        play()
    }

    fun play() {
        isAutoPlayNext = true
        if (musicList.size == 0) {
            return
        }
        val path = musicList[current]
        mediaPlayer.reset()
        try {
            mediaPlayer.setDataSource(path)
            mediaPlayer.prepareAsync()
            mediaPlayer.setOnPreparedListener {
                it.start()
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onBind(intent: Intent): IBinder {
        return binder
    }

    private fun getMusicList() {
        val downloadUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
        val selection = "${MediaStore.Audio.Media.DATA} LIKE ?"
        val selectionArgs = arrayOf("%Download%")
        val cursor = contentResolver.query(downloadUri, null, selection, selectionArgs, null)

        if (cursor != null) {
            while (cursor.moveToNext()) {
                val dataIndex = cursor.getColumnIndex(MediaStore.Audio.Media.DATA)
                val titleIndex = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)

                if (dataIndex != -1 && titleIndex != -1) {
                    val musicPath = cursor.getString(dataIndex)
                    musicList.add(musicPath)
                    val musicName = cursor.getString(titleIndex)
                    musicNameList.add(musicName)
                    Log.d(TAG, "getMusicList: $musicPath name: $musicName")
                } else {
                    Log.e(TAG, "Column not found")
                }
            }
            cursor.close()
        }
    }

}